#!/usr/bin/env python

# Demonstrate how to use the vtkBoxWidget to control volume rendering
# within the interior of the widget.

import vtk
import myVTKRenderWindowInteractorStyle


#Create the volume and volume reader
#This might be a problem if we want to register a 3D object other than a volume (eg. polygonal mesh)
filename = 'C:/Lib/jointtrack/branches/TestData/vert1.vtk'
volume = vtk.vtkVolume()
volumeReader = vtk.vtkStructuredPointsReader()
volumeReader.SetFileName(filename)
volumeReader.Update()

#TODO:Allow the user to modify/customize the transfer functions to change how the DRR is rendered!!
#Default settings for the opacity transfer function,
opacityTransferFunction = vtk.vtkPiecewiseFunction()
opacityTransferFunction.AddPoint(0.0, 0.0)
opacityTransferFunction.AddPoint(70, 0.0)
opacityTransferFunction.AddPoint(255, 0.5)

# Create transfer mapping scalar value to color
colorTransferFunction = vtk.vtkColorTransferFunction()
colorTransferFunction.AddRGBPoint(0.0, 0.0, 0.0, 0.0)
colorTransferFunction.AddRGBPoint(72, 1.0, 1.0, 1.0)
colorTransferFunction.AddRGBPoint(255, 1.0, 1.0, 1.0)

# The property describes how the data will look
volumeProperty = vtk.vtkVolumeProperty()

# Setup default properties
volumeProperty.SetColor(colorTransferFunction)
volumeProperty.SetScalarOpacity(opacityTransferFunction)
volumeProperty.ShadeOn()
volumeProperty.SetInterpolationTypeToLinear()

volumeMapper = vtk.vtkVolumeTextureMapper3D()
volumeMapper.SetInput(volumeReader.GetOutput())

volume.SetProperty(volumeProperty)
volume.SetMapper(volumeMapper)
#______________________________________________________________
# start vtk example code

newvol = vtk.vtkVolume()
newvol.SetMapper(volumeMapper)
newvol.SetProperty(volumeProperty)

outline = vtk.vtkOutlineFilter()
outline.SetInputConnection(volumeReader.GetOutputPort())
outlineMapper = vtk.vtkPolyDataMapper()
outlineMapper.SetInputConnection(outline.GetOutputPort())
outlineActor = vtk.vtkActor()
outlineActor.SetMapper(outlineMapper)

# Create the RenderWindow, Renderer and both Actors
ren = vtk.vtkRenderer()
renWin = vtk.vtkRenderWindow()
renWin.AddRenderer(ren)
iren = vtk.vtkRenderWindowInteractor()
iren.SetRenderWindow(renWin)

#Set Custom interaction style
style = myVTKRenderWindowInteractorStyle.myVTKRenderWindowInteractorStyle(iren)
iren.SetInteractorStyle(style)

# The SetInteractor method is how 3D widgets are associated with the
# render window interactor. Internally, SetInteractor sets up a bunch
# of callbacks using the Command/Observer mechanism (AddObserver()).
boxWidget = vtk.vtkBoxWidget()
boxWidget.SetInteractor(iren)
boxWidget.SetPlaceFactor(1.0)

# Add the actors to the renderer, set the background and size
#ren.AddActor(outlineActor)
ren.AddVolume(newvol)

ren.SetBackground(0, 0, 0)
renWin.SetSize(512, 512)

# When interaction starts, the requested frame rate is increased.
def StartInteraction(obj, event):
    global renWin
    renWin.SetDesiredUpdateRate(10)

# When interaction ends, the requested frame rate is decreased to
# normal levels. This causes a full resolution render to occur.
def EndInteraction(obj, event):
    global renWin
    renWin.SetDesiredUpdateRate(0.001)

# The implicit function vtkPlanes is used in conjunction with the
# volume ray cast mapper to limit which portion of the volume is
# volume rendered.
planes = vtk.vtkPlanes()
def ClipVolumeRender(obj, event):
    global planes, volumeMapper
    obj.GetPlanes(planes)
    volumeMapper.SetClippingPlanes(planes)


# Place the interactor initially. The output of the reader is used to
# place the box widget.
boxWidget.SetInput(volumeReader.GetOutput())
boxWidget.PlaceWidget()
boxWidget.InsideOutOn()
boxWidget.AddObserver("StartInteractionEvent", StartInteraction)
boxWidget.AddObserver("InteractionEvent", ClipVolumeRender)
boxWidget.AddObserver("EndInteractionEvent", EndInteraction)

outlineProperty = boxWidget.GetOutlineProperty()
outlineProperty.SetRepresentationToWireframe()
outlineProperty.SetAmbient(1.0)
outlineProperty.SetAmbientColor(1, 1, 1)
outlineProperty.SetLineWidth(1)

selectedOutlineProperty = boxWidget.GetSelectedOutlineProperty()
selectedOutlineProperty.SetRepresentationToWireframe()
selectedOutlineProperty.SetAmbient(1.0)
selectedOutlineProperty.SetAmbientColor(1, 0, 0)
selectedOutlineProperty.SetLineWidth(3)


iren.Initialize()
renWin.Render()
iren.Start()
